﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.SqlClient;
using System.Data.Common;
using System.Diagnostics;

namespace DbSharp.Tools.DbConnectionBuilder.Core.Plugin
{
  
    class MsSqlConnectionPlugin : IDbConnectionPlugin
    { 
        #region IDbConnectionPlugin Members

        public PluginNamePair PluginNamePair
        {
            get { return new PluginNamePair("System.Data.SqlClient") { DisplayName = "MS SQL Server Driver" }; }
        }

        public DbConnectionStringBuilder CreateConnectionStrBuilder(DbServerCredential credential)
        {
            //Data Source=SHNI317\SQLEXPRESS;Initial Catalog=AdventureWorks;Integrated Security=True
            var builder = new SqlConnectionStringBuilder();
            builder.DataSource = credential.DataSource;
            builder.IntegratedSecurity = credential.OsInteAuth;
            if (builder.IntegratedSecurity == false)
            {
                builder.UserID = credential.UserId;
                builder.Password = credential.Password;
            }
            builder.InitialCatalog = credential.Database;

            return builder;
        }

        public DbServerCredential CreateCredential(string connectionString)
        {
            DbServerCredential credential = new DbServerCredential();
            credential.UserIdEnabled = true;
            credential.PasswordEnabled = true;
            credential.DataSourceEnabled = true;
            credential.DatabaseEnabled = true;
            credential.OsInteAuthEnabled = true;

            var builder = new SqlConnectionStringBuilder(connectionString);
            object userId;
            if (builder.TryGetValue("User ID", out userId))
            {
                credential.UserId = userId.ToString();
            }
            object pwd;
            if (builder.TryGetValue("Password", out pwd))
            {
                credential.Password = pwd.ToString();
            }
            object dataSource;
            if (builder.TryGetValue("Data Source", out dataSource))
            {
                credential.DataSource = dataSource.ToString();
            }
            object database;
            if (builder.TryGetValue("Initial Catalog", out database))
            {
                credential.Database = database.ToString();
            }

            //Integrated Security
            object integratedSecurity;
            if (builder.TryGetValue("Integrated Security", out integratedSecurity))
            {
                credential.OsInteAuth = (bool)integratedSecurity;
            }

            credential.AdditionalInfo = "";
            return credential;
        }

        public DbConnection CreateDbConnection(string connectionString)
        {
            return new SqlConnection(connectionString);
        }

        public List<FileVersionPair> GetCandidateDbFileVersionPairs()
        {
            var list = new List<FileVersionPair>();
            return list;
        }


        #endregion
    }
}
